package com.redwood.report.activity.bak;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.redwood.report.ItemDecoration.SpaceItemDecoration;
import com.redwood.report.R;
import com.redwood.report.activity.mvp.login.LoginActivity;
import com.redwood.report.adapter.bak.DemoMainAdapter;
import com.redwood.report.base.BaseActivity;
import com.redwood.report.entity.ServiceResult;
import com.redwood.report.entity.UpdateInfo;
import com.redwood.report.entity.bak.demo.Caption;
import com.redwood.report.net.Api;
import com.redwood.report.net.NetUtils;
import com.redwood.report.util.Utils;
import com.redwood.report.widget.dialog.CUpdateDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivityBak extends BaseActivity {

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    private BaseQuickAdapter homeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        unbinder = ButterKnife.bind(this);
        initView();
        initData();

    }

    private void initData() {
        List<Caption> captionList = new ArrayList<>();
        Caption caption1 = new Caption("登录示列Mvp短信集成", LoginActivity.class);
        captionList.add(caption1);
        Caption caption6 = new Caption("WebView手机适配", TestWebViewActivity.class);
        captionList.add(caption6);
        homeAdapter.setNewData(captionList);
    }

    private void initView() {
        initAdapter();
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"click",0).show();
            }
        });
    }

    private void initAdapter() {
        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        //设置RecyclerView分割线
        rvList.addItemDecoration(new SpaceItemDecoration(getActivity()).setSpace(0.5f).setSpaceColor(R.color.bggray));
        homeAdapter = new DemoMainAdapter();
        //item点击事件
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mContext.startActivity(new Intent(mContext, ((Caption) homeAdapter.getData().get(position)).getMclass()));
//                if(position%2==0) {
//                    ToolbarUtil.setColorNoTranslucent(getActivity(), getResources().getColor(R.color.white));
//                    LightStatusBarUtils.setLightStatusBar(getActivity(), true);
//                }else{
//                    //标题栏颜色
//                    ToolbarUtil.setColorNoTranslucent(getActivity(), getResources().getColor(R.color.colorAppKey));
//                    //深色主题 true字颜色为深色 false 为白色标题字
//                    LightStatusBarUtils.setLightStatusBar(getActivity(), false);
//                }
            }
        });
        //item子控件点击事件  需要在设置控件数据时 add id
        homeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        rvList.setAdapter(homeAdapter);
    }

    /**
     * 获取更新
     */
    public void getUpdateAutoCheck() {
        Api.checkUpdate(getActivity(), new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                UpdateInfo update = (UpdateInfo) rspData;
                if (update.getRecode()==1) {
                    int versionCode = update.getVersionCode();
                    int currVer = Utils.getClientVersionCode(getApplication());
                    if (versionCode > currVer) {
                        showUpdateDialog(update);
                    }
                }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, UpdateInfo.class);
    }

    /**
     * 显示软件下载对话框，在线更新
     */
    private void showUpdateDialog(UpdateInfo update) {
        CUpdateDialog dialog = new CUpdateDialog(this, update);
        dialog.show();
    }

}
